﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Polenter.Serialization;

namespace ZKCache
{
    public class CacheDictionary
    {
        public string identifier { get; set; }
        public string cache_create_time { get; set; }
        public float expired_time { get; set; }
        public string cachePath { get; set; }
        
        public T objectFromCache<T>()
        {
            T cacheObj = default(T);
            if(!this.IsExpired && File.Exists(cachePath + identifier + ".cache"))
            {
                using(FileStream fs = new FileStream(cachePath + identifier + ".cache",FileMode.Open))
                {
                    SharpSerializer serializer = new SharpSerializer();
                    cacheObj = (T)serializer.Deserialize(fs);
                }
            }
            return cacheObj;
        }

        public bool IsExpired
        {
            get
            {
                DateTime expiredDate = Convert.ToDateTime(cache_create_time).AddMinutes(expired_time);
                return expiredDate <= DateTime.Now;
            }
        }
    }
}
